﻿$(function () {
    loadMenu();
    getCurrentcy();
    window.setTimeout(function () {
        $("textarea").height($("textarea")[0].scrollHeight);
    }, 1);
});


vm.Jobdetail = ko.observable();
vm.RelatedJobs = ko.observableArray();
vm.isApply = ko.observable(false);
vm.isEdit = ko.observable(false);
vm.Employee = ko.observableArray();
vm.basicInfor = function (data) {


    ko.cleanNode($('#infor')[0]);
    ko.applyBindings(data, $('#infor')[0]);
    $('#infor').modal();
}
vm.applyJob = function () {
    $('#_loading').show();
    $('#applyjob').prop('disabled', true);
    var apply = manager.createEntity('ApplyJob', { PersonalId: $('#_perid').text(), JobId: jobcode, ApplyDate: new Date($.now()) });
    saveSingleEntity(apply, null);    
    $('#applyjob').prop('disabled', false);
    $('#_loading').hide();
    vm.isApply(true);
}
vm.modifyJob = function (entity) {
    $('#_mdloadind').show();
    entity.LocationId(entity.city());
    if (entity.competitive()) {
        entity.SalaryFrom('Competitive');
    }
    entity.IndustryId(entity.func());
    manager.saveChanges([entity]).then(function () {
        $('#_mdloadind').hide();
    }).fail(saveFailed);
}
vm.Location = ko.observableArray();
vm.Province = ko.observableArray();
vm.City = ko.observableArray();
vm.Industry = ko.observableArray();
vm.Function = ko.observableArray();
vm.Currency = ko.observableArray();

function getCurrentcy() {
    var qrAppliedJob = new breeze.EntityQuery().from("Currencies");
    manager.executeQuery(qrAppliedJob).then(function (data) {
        data.results.forEach(function (item) {
            vm.Currency.push(item);
        });
    }).fail(function (e) {
        alert(e);
    });
}
function getLocation() {
    var qrLocation = new breeze.EntityQuery().from("Locations").where("ParentId", "==", null);
    manager.executeQuery(qrLocation).then(function (data) {
        data.results.forEach(function (item) {
            vm.Location.push(item);
        });
    }).fail(function (e) {
        alert(e);
    });
}
function getIndustry() {
    var qrIndustry = new breeze.EntityQuery().from("Industries").where("ParentId", "==", null);
    manager.executeQuery(qrIndustry).then(function (data) {
        data.results.forEach(function (item) {
            vm.Industry.push(item);
        });
    }).fail(function (e) {
        alert(e);
    });
}
function changeCountry(value) {
    vm.Province([]);
    var query = new breeze.EntityQuery
              .from('Location')
              .where('ParentId', '==', value);
    var provinces = manager.executeQueryLocally(query);
    for (var i = 0; i < provinces.length; i++) {
        vm.Province.push(provinces[i]);
    }
}
function changeProvince(value) {
    vm.City([]);
    var qr = new breeze.EntityQuery
              .from('Location')
              .where('ParentId', '==', value);
    var city = manager.executeQueryLocally(qr);
    for (var i = 0; i < city.length; i++) {
        vm.City.push(city[i]);
    }
}
function changeIndustry(value) {
    vm.Function([]);
    var query = new breeze.EntityQuery
              .from('Industry')
              .where('ParentId', '==', value);
    var functions = manager.executeQueryLocally(query);
    for (var i = 0; i < functions.length; i++) {
        vm.Function.push(functions[i]);
    }

}


vm.view = function () {
    //vm.Jobdetail
    vm.Jobdetail().country = ko.observable();
    vm.Jobdetail().province = ko.observable();
    vm.Jobdetail().city = ko.observable();
    vm.Jobdetail().industry = ko.observable('');
    vm.Jobdetail().func = ko.observable();

    ko.cleanNode($('#priModal')[0]);
    vm.Jobdetail().country(vm.Jobdetail().Location().Parent().ParentId());
    vm.Jobdetail().province(vm.Jobdetail().Location().ParentId());
    vm.Jobdetail().city(vm.Jobdetail().LocationId());

    vm.Jobdetail().industry(vm.Jobdetail().Industry().ParentId());
    vm.Jobdetail().func(vm.Jobdetail().IndustryId());

    changeCountry(vm.Jobdetail().Location().Parent().ParentId());
    changeProvince(vm.Jobdetail().Location().ParentId());
    changeIndustry(vm.Jobdetail().Industry().ParentId());

    //Add subcriber
    vm.Jobdetail().country.subscribe(function (newValue) {
        changeCountry(newValue);
    });
    vm.Jobdetail().province.subscribe(function (newValue) {
        if (typeof newValue != 'undefined')
            changeProvince(newValue);
    });
    vm.Jobdetail().industry.subscribe(function (newValue) {
        changeIndustry(newValue);
    });



    vm.Jobdetail().competitive = ko.observable(false);
    vm.Jobdetail().competitive.subscribe(function (newValue) {
        if (newValue) {
            $('#msalary1').prop('disabled', true);
            $('#msalary2').prop('disabled', true);
            $('#msalary3').prop('disabled', true);
        } else {
            $('#msalary1').prop('disabled', false);
            $('#msalary2').prop('disabled', false);
            $('#msalary3').prop('disabled', false);
        }
    });

    vm.Jobdetail().StandardSalary = ko.observable('');
    if (vm.Jobdetail().SalaryFrom() == "Competitive") {
        vm.Jobdetail().competitive(true);
        $('#msalary1').prop('disabled', true);
        $('#msalary2').prop('disabled', true);
        $('#msalary3').prop('disabled', true);


    } else {
        vm.Jobdetail().StandardSalary(addCommas(vm.Jobdetail().SalaryFrom()) + " - " + addCommas(vm.Jobdetail().SalaryTo()));
    }


    $('#priModal').modal();
    ko.applyBindings(vm.Jobdetail(), $('#priModal')[0]);

}


function isApply(jobcode, personalid) {
    var p1 = new breeze.Predicate.create("PersonalId", "==", personalid);
    var p2 = new breeze.Predicate.create("JobId", "==", jobcode);

    var query = new breeze.EntityQuery
                  .from('ApplyJobs')
                  .where(p1.and(p2));
    manager.executeQuery(query).then(function (data) {
        if (data.results.length > 0) vm.isApply(true);
        else vm.isApply(false);
    }).fail(function (e) {
        alert(e);
    });  
}

function getRelatedJobs() {
    var qrrelatedjobs = new breeze.EntityQuery().from("Jobs").where("Industry.ParentId", "==", vm.Jobdetail().Industry().ParentId());
    manager.executeQuery(qrrelatedjobs).then(function (dt) {
        dt.results.forEach(function (ite) {
            if (ite.Name() != vm.Jobdetail().Name() && vm.RelatedJobs().length <= 5) {
                ite.StandardSalary = addCommas(ite.SalaryFrom()) + " - " + addCommas(ite.SalaryTo());
                vm.RelatedJobs.push(ite);
            }
        });
    }).fail(function (e) {
        alert(e);
    });
}
function getEmployee() {
    var qremployee = new breeze.EntityQuery().from("ApplyJobs").where("JobId", "==", jobcode);
    manager.executeQuery(qremployee).then(function (dt) {
        dt.results.forEach(function (ite) {
            vm.Employee.push(ite.PersonalInfor());
        });
    }).fail(function (e) {
        alert(e);
    });

}

var jobcode = GetURLParameter('jobcode');
if (jobcode != null) {
    var detailquery = new breeze.EntityQuery().from("Jobs").where("UniqueId", "==", parseInt(jobcode));

    manager.executeQuery(detailquery).then(function (data) {
        data.results.forEach(function (item) {
            item.StandardSalary = addCommas(item.SalaryFrom()) + " - " + addCommas(item.SalaryTo());
            vm.Jobdetail(item);
            if (item.PersonalId() == $.trim($('#_perid').text())) {
                vm.isEdit(true);
            }
        });
        //Get related jobs
        getRelatedJobs();
        if ($.trim($('#_perid').text()) != "") {
            isApply(jobcode, $.trim($('#_perid').text()));
            if (vm.Role() == 'Employer' && vm.Jobdetail().PersonalInfor().UniqueId() == $.trim($('#_perid').text())) {
                getLocation();
                getIndustry();
                getEmployee();
            }
        }

        ko.cleanNode($('#detailinformation')[0]);
        ko.applyBindings(vm, $('#detailinformation')[0]);

    }).fail(function (e) {
        alert(e);
    });
}

    


